package com.gs.SynAntiWandering.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.res.ResourcesCompat;

import com.gs.SynAntiWandering.R;

public class InsightView extends FrameLayout {

    private TextView tvNum,tvTop;

    public InsightView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public InsightView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public InsightView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public InsightView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InsightView);
        String title = ta.getString(R.styleable.InsightView_insight_title);
//        int color = ta.getInteger(R.styleable.InsightView_insight_color, R.color.red);
//        int drawable = ta.getResourceId(R.styleable.InsightView_insight_drawable, R.drawable.oval_stoke_green_bg);
        View view = LayoutInflater.from(context).inflate(R.layout.item_insight, this, false);
        tvTop = view.findViewById(R.id.tv_top);
        tvTop.setText(title);
        tvNum = view.findViewById(R.id.tv_third);
//        tvNum.setTextColor(color);
//        tvNum.setBackground(ResourcesCompat.getDrawable(context.getResources(), drawable, null));
        ta.recycle();
        addView(view);
    }

    public void setNum(String num) {
        tvNum.setText(num);
    }

    public void setTitle(String text){
        tvTop.setText(text);
    }

    public void setTextColor(int color){
        tvNum.setTextColor(getContext().getResources().getColor(color));
    }

    public void setDrawable(int resource){
        tvNum.setBackground(ResourcesCompat.getDrawable(getContext().getResources(), resource, null));
    }
}
